package com.sgb.member.view_model;

import android.text.TextUtils;

import com.sgb.member.MemberConfig;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.entity.MemberDemandBean;
import com.sgb.member.entity.MemberDemandLeaseBean;
import com.sgb.member.ui.fragment.MemberDemandFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * Time:2021/4/30
 * AUTHOR:F_Q
 * Description:
 */
public class MemberDemandModel extends BaseViewModel {

    MemberDemandFragment fragment;
    MemberRepertory repertory;
    private int current;
    public MemberDemandModel(MemberDemandFragment fragment, MemberRepertory repertory) {
        this.fragment = fragment;
        this.repertory=repertory;
    }

    public void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
            if (!DialogHelper.isShowing()) {
                DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...");
            }
        } else {
            DialogHelper.dismissProgressDialog();
        }
    }

    public void getAllLeaseType() {
        // 1机械， 2 设备，3车辆，4 房产，5土地，6工厂，7农机
        // "工程机械", "工程设备", "车辆租赁", "房产租赁", "土地租赁", "工厂租赁", "农机租赁"、
        showOrHideDialog(true);
        repertory.getApi().getRentCategory().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setChooseItemData(data.getData());
                } else {
                    showOrHideDialog(false);
                    fragment.showEmpty();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CommonPopEntity>>> call, Throwable t) {
                super.onFailure(call, t);
                showOrHideDialog(false);
                fragment.showEmpty();
            }

            @Override
            public void onFinally() {

            }
        });
    }

    //列表信息
    public void getDemandList(boolean isLoadMore, int mCurPos, String text, String tabNo) {
        showOrHideDialog(true);
        int currentPage = isLoadMore ? current + 1 : 1;
        showOrHideDialog(true);
        Map<String, Object> map = new HashMap<>();
        map.put("page", currentPage);
        int maxPageSize = 10;
        map.put("limit", maxPageSize);
        map.put("keyWord", TextUtils.isEmpty(text) ? "" : text);
        Call<BaseEntity<ListMResponse<List<MemberDemandBean>>>> call;
        if (mCurPos == 0) {
            //会员需求商品询价列表
            repertory.getApi().getGoodsEnquiryPriceList(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberDemandBean>>>>() {
                @Override
                public void onSuccess(BaseEntity<ListMResponse<List<MemberDemandBean>>> data) {
                    current = data.getData().getCurrent();
                    int total = data.getData().getPages();
                    fragment.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
                }

                @Override
                public void onFailure(Call<BaseEntity<ListMResponse<List<MemberDemandBean>>>> call, Throwable t) {
                    super.onFailure(call, t);
                    if (!isLoadMore) {
                        fragment.showEmpty();
                    }
                }
            });;
        } else {
            //会员需求求租需求
            map.put("rentSeekCategory", tabNo); //  //0机械求租 1设备求租 2车辆求租 3房产求租 4土地求租 5工厂求租 6农机求租
            map.put("type", tabNo); //  //0机械求租 1设备求租 2车辆求租 3房产求租 4土地求租 5工厂求租 6农机求租
            map.put("status", "1"); // 求组中
            repertory.getApi().getRentSeeksNeedList(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberDemandLeaseBean>>>>() {
                @Override
                public void onSuccess(BaseEntity<ListMResponse<List<MemberDemandLeaseBean>>> data) {
                    current = data.getData().getCurrent();
                    int total = data.getData().getPages();
                    fragment.refreshRecyclerLease(data.getData().getList(), isLoadMore, current >= total);
                }

                @Override
                public void onFailure(Call<BaseEntity<ListMResponse<List<MemberDemandLeaseBean>>>> call, Throwable t) {
                    super.onFailure(call, t);
                    if (!isLoadMore) {
                        fragment.showEmpty();
                    }
                }
            });;
        }
    }

    //获取H5链接
    public void webH5(int curPos, String no, int type) {
        showOrHideDialog(true);
        Call<BaseEntity<H5ReEntity>> call = null;
        if (curPos == 0) {
            call = repertory.getApi().getUrlSetting("enquiryDetail");
        } else {
            String rentSeekCategory = "";
            switch (type) {
                case 0: // 0机械求租
                    rentSeekCategory = "mechanicsRentDetails";
                    break;
                case 1: // 1设备求租
                    rentSeekCategory = "equipmentRentDetails";
                    break;
                case 2: // 2车辆求租
                    rentSeekCategory = "vehicleRentDetails";
                    break;
                case 3: // 3房产求租
                    rentSeekCategory = "houseRentDetails";
                    break;
                case 4: // 4土地求租
                    rentSeekCategory = "landRentDetails";
                    break;
                case 5: // 5工厂求租
                    rentSeekCategory = "factoryRentDetails";
                    break;
                case 6: // 农机求租
                    rentSeekCategory = "farmRentDetails";
                    break;
            }
            if (!TextUtils.isEmpty(rentSeekCategory)) {
                call = repertory.getApi().getUrlSetting(rentSeekCategory);
            }
        }
        if (call == null) {
            showOrHideDialog(false);
            return;
        }
        call.enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String getParam;
                    if (curPos == 0) {
                        getParam = "?no=" + no;
                        //存储商品跳转h5标记
                        SharedPreferenceUtil.getInstance().saveString("EVENT_SHOPPING_TYPE", "event_shopping");
                    } else {
                        getParam = "?id=" + no;
                    }
                    String url = data.getData().getUrl() + getParam;
                    MemberConfig.getInstance().startWebViewX5(fragment.getActivity(), url);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }
}
